package com.xy.handler;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.xy.constants.ControlCharacter;
import com.xy.constants.RabbitConstant;
import com.xy.constants.RedisConstant;
import com.xy.entity.AbnormalRecordEntity;
import com.xy.entity.GeneralRecordEntity;
import com.xy.entity.PositionEntity;
import com.xy.entity.PositionGroupEntity;
import com.xy.request.BaseRequest;
import com.xy.response.InspectResponse;
import com.xy.response.ViewerResponse;
import com.xy.service.IAbnormalRecordService;
import com.xy.service.IGeneralRecordService;
import com.xy.service.IPositionGroupService;
import com.xy.service.IPositionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@AllArgsConstructor
public class QueueInspectRecord {
	private final BladeRedis bladeRedis;
	private final IAbnormalRecordService abnormalRecordService;
	private final IGeneralRecordService generalRecordService;
	private final IPositionService positionService;
	private final IPositionGroupService positionGroupService;



	@RabbitListener(queues = RabbitConstant.TOPIC_RECORD_QUEUE)
	@RabbitHandler
	public void queuqRecord(String str, Message message, Channel channel) {
		log.info("队列1门禁记录，接收消息：{}", str);
		JSONObject jsonObject = JSONObject.parseObject(str);
		InspectResponse inspectResponse = jsonObject.getObject("inspectResponse", InspectResponse.class);
		if (Func.isEmpty(inspectResponse.getOauthKey())) {
			log.info("鉴权key为空、提交的门禁记录参数为：{}", jsonObject);
			return;
		}
		List<ViewerResponse> viewerResponseList = bladeRedis.get(RedisConstant.OAUTH_KEY.concat(inspectResponse.getOauthKey()));
		if (Func.isEmpty(viewerResponseList)) {
			log.info("门禁记录重复提交：提交的key：{}", inspectResponse.getOauthKey());
			return;
		}
		bladeRedis.del(RedisConstant.OAUTH_KEY.concat(inspectResponse.getOauthKey()));
		BaseRequest baseRequest = jsonObject.getObject("baseRequest", BaseRequest.class);
		List<ViewerResponse> responseList = viewerResponseList.stream().filter(o -> o.getWhetherOrNot().equals(ControlCharacter.WHETHER_OR_YES)).collect(Collectors.toList());

		if (Func.isNotEmpty(responseList)) {
			//正常门禁集合
			List<GeneralRecordEntity> recordEntityList = new ArrayList<>();
			responseList.forEach(o -> {
				//数据组装
				dataRecordList(recordEntityList, o, inspectResponse, baseRequest);
			});
			generalRecordService.saveBatch(recordEntityList);
			return;
		}
		//非正常门禁集合
		List<AbnormalRecordEntity> abnormalRecordEntityList = new ArrayList<>();
		viewerResponseList.forEach(o -> {

			dataIllegaList(abnormalRecordEntityList, o, inspectResponse, baseRequest);
			return;
		});
		if (CollectionUtil.isNotEmpty(abnormalRecordEntityList)) {
			abnormalRecordService.saveBatch(abnormalRecordEntityList);
		}
	}

	/**
	 * 非法数据组装
	 *
	 * @param o
	 */
	public void dataIllegaList(List<AbnormalRecordEntity> abnormalRecordEntityList, ViewerResponse o, InspectResponse inspectResponse, BaseRequest baseRequest) {
		AbnormalRecordEntity abnormalRecord = BeanUtil.instantiateClass(AbnormalRecordEntity.class);
		abnormalRecord.setIllicitViewer(inspectResponse.getRecordViewer());
		abnormalRecord.setExhibitionId(o.getExhibitionId());
		abnormalRecord.setExhibitionName(o.getExhibitionName());
		abnormalRecord.setAbnormalContent(o.getMessage());
		abnormalRecord.setAbnormalType(o.getErrorType() + "");
		abnormalRecord.setOauthKey(inspectResponse.getOauthKey());
		abnormalRecord.setPositionGroupId(baseRequest.getPositionGroupId());
		abnormalRecord.setPositionId(baseRequest.getPositionId());
		PositionEntity entity = positionService.getById(baseRequest.getPositionId());
		abnormalRecord.setPositionName(entity.getPositionName());
		PositionGroupEntity groupEntity = positionGroupService.getById(baseRequest.getPositionGroupId());
		abnormalRecord.setPositionGroupName(groupEntity.getGroupName());
		abnormalRecordEntityList.add(abnormalRecord);
	}

	/**
	 * 正常数据组装
	 *
	 * @param
	 * @param o
	 */
	public void dataRecordList(List<GeneralRecordEntity> recordEntityList, ViewerResponse o, InspectResponse inspectResponse, BaseRequest baseRequest) {
		GeneralRecordEntity generalRecordEntity = BeanUtil.instantiateClass(GeneralRecordEntity.class);
		generalRecordEntity.setExhibitionId(o.getExhibitionId());
		generalRecordEntity.setExhibitionName(o.getExhibitionName());
		generalRecordEntity.setViewerId(o.getViewerId());
		generalRecordEntity.setViewerName(o.getViewerName());
		generalRecordEntity.setViewerTypeId(o.getViewerTypeId());
		generalRecordEntity.setViewerTypeName(o.getViewerTypeName());
		generalRecordEntity.setOauthKey(inspectResponse.getOauthKey());
		generalRecordEntity.setPositionGroupId(baseRequest.getPositionGroupId());
		generalRecordEntity.setPositionId(baseRequest.getPositionId());
		generalRecordEntity.setGeneralProof(inspectResponse.getRecordViewer());
		generalRecordEntity.setPassTime(new Date());
		generalRecordEntity.setSourceFrom("1");
		PositionEntity entity = positionService.getById(baseRequest.getPositionId());
		generalRecordEntity.setPositionName(entity.getPositionName());
		PositionGroupEntity groupEntity = positionGroupService.getById(baseRequest.getPositionGroupId());
		generalRecordEntity.setGroupName(groupEntity.getGroupName());
		recordEntityList.add(generalRecordEntity);
	}
}
